<?php

namespace App\Models\GameServer;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class LoginRecord extends Model
{
    protected $table = 'login_record';
    protected $primaryKey = 'account_id';
    protected $connection = 'game_server';

    protected $appends = [
        // 'online_time'
    ];

    //计算在线时间
    public function getOnlineTimeAttribute()
    {
        $login_time = Carbon::createFromTimestamp($this->attributes['login_time']);

        if ($this->attributes['logout_time'] == 0) {
            $logout_time = Carbon::now();
        } else {
            $logout_time = Carbon::createFromTimestamp($this->attributes['logout_time']);
        }


        $online_time = $login_time->diffForHumans($logout_time, true);

        return $online_time;
    }
}
